<template>
  <div class="departments-container">
    <div class="app-container">
      <el-card v-loading="loading" class="tree-card">
        <!-- 用了一个行列布局 -->
        <!-- T -->
        <tree-tools :tree-node="company" :is-root="true" />
        <!-- !--放置一个属性   这里的props和我们之前学习的父传子 的props没关系-->
        <el-tree :data="depts" :props="defaultProps" :default-expand-all="true">
          <!-- 传入内容 插槽内容 会循环多次 有多少节点 就循环多少次 -->
          <!-- 作用域插槽 slot-scope="obj" 接收传递给插槽的数据   data 每个节点的数据对象-->
          <!-- TAG -->
          <tree-tools
            slot-scope="{data}"
            :tree-node="data"
            @delDepts="getDepartments"
            @addDepts="addDepts"
            @editDepts="editDepts"
          />
        </el-tree>
      </el-card>
    </div>
    <!-- 添加部门的弹层 -->
    <add-dept
      ref="addDept"
      :show-dialog.sync="showDialog"
      :tree-node="node"
      @close="showDialog = false"
      @addDepts="getDepartments"
    />
  </div>
</template>

<script>
import { getDepartments } from '@/api/departments'
import TreeTools from './components/tree-tools.vue'
import { tranListToTreeData } from '@/utils/index'
import AddDept from './components/add-dept.vue'
export default {
  name: 'Departments',
  components: { TreeTools, AddDept },
  props: {},
  data () {
    return {
      defaultProps: {
        label: 'name'
      },
      company: { },
      depts: [
      ], // 部门表
      loading: true,
      showDialog: false, // 添加部门的显示状态
      node: {}
    }
  },
  computed: {},
  watch: {},
  created () {
    this.getDepartments()
  },
  mounted () {},
  methods: {
    async getDepartments () {
      try {
        const result = await getDepartments()
        this.company = { name: result.companyName, manager: '负责人', id: '' }
        // console.log(result)
        this.depts = tranListToTreeData(result.depts, '')
        this.loading = false
      } catch (err) {
        this.$message.error('获取组织架构列表失败')
      }
    },
    addDepts (node) {
      this.showDialog = true
      this.node = node
    },
    editDepts (node) {
      this.showDialog = true
      this.node = node
      // 调用组件中的方法获取你点的数据
      this.$refs.addDept.getDepartDetail(node.id)
      // console.log(node)
    }
  }
}
</script>

<style scoped lang="scss">
.tree-card {
  padding: 30px  140px;
  font-size:15px;
  .title {
    cursor: pointer;
  }
}
</style>
